package com.lyn.tool.mvp.other;


import com.lyn.tool.mvp.contract.MainContract;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author Lyn
 * @date 2022/5/22
 * @desc
 */
public class ExecutorAsyncTask {

   public static void newExecutor(MainContract.View view,MainContract.AsyncTask<Result> rAsyncTask){
       AsyncTask<Result,Integer> asyncTask=new AsyncTask<Result,Integer>() {

           @Override
           protected void onPreExecute() {
               super.onPreExecute();
               view.onLoading();
           }

           @Override
           protected Result doInBackground() {
               return rAsyncTask.doInBackground();
           }

           @Override
           protected void onProgressUpdate(Integer p) {
               super.onProgressUpdate(p);
           }

           @Override
           protected void onPostExecute(Result R) {
               super.onPostExecute(R);
               view.onHideLoading();
               view.onResult(R);
           }
       };
       asyncTask.execute(ThreadExecutor.getInstance());
   }

    public static void newExecutor(MainContract.View view, MainContract.AsyncTask<Result> rAsyncTask
            , ThreadPoolExecutor threadPoolExecutor){
        AsyncTask<Result,Integer> asyncTask=new AsyncTask<Result,Integer>() {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                view.onLoading();
            }

            @Override
            protected Result doInBackground() {
                return rAsyncTask.doInBackground();
            }

            @Override
            protected void onProgressUpdate(Integer p) {
                super.onProgressUpdate(p);
            }

            @Override
            protected void onPostExecute(Result R) {
                super.onPostExecute(R);
                view.onHideLoading();
                view.onResult(R);
            }
        };
        asyncTask.execute(threadPoolExecutor);
    }
}
